package com.yyk.yykywd.util;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间转换器
 */

public class TimeUtil {
    public static final String ENG_DATE_FROMAT = "EEE, d MMM yyyy HH:mm:ss z";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_TEXT = "yyyy年MM月dd日";
    public static final String YYYY = "yyyy";
    public static final String MM = "MM";
    public static final String DD = "dd";
    public static final String MM_DD_HH_MM_TEXT = "MM月dd日 HH:mm";
    public static final String MM_DD_TEXT = "MM月dd日";

    public static final String HH_MM = "HH:mm";
    public static final String YYYY_MM = "yyyy-MM";

    private static TimeUtil timeUtil;

    private TimeUtil() {

    }

    /**
     * 单例模式获取Dialog工具类
     *
     * @return
     */
    public static TimeUtil getInstance() {
        if (null == timeUtil) {
            timeUtil = new TimeUtil();
        }
        return timeUtil;
    }

    /**
     * 可根据需要自行截取数据显示
     *
     * @param
     * @return
     */

    public String getMmDd(int timeer) {
        //        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("MM/dd");
        String time = format.format(new Date(Long.valueOf(timeer) * 1000L));
        return time;
    }

    /**
     * 时间格式 1   值为  秒
     * 暂时有点问题，返回时间不准确
     *
     * @return
     */
    public String getYyyyMmDdHhMmSs(int timeer) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        String time = format.format(new Date(Long.valueOf(timeer) * 1000L));
        Log.d("xxx-------->", "转换后时间: " + time);
        return time;
    }

    /**
     * 时间格式 1   值为  秒
     * 暂时有点问题，返回时间不准确
     *
     * @return
     */
    public String getYyyyMmDd(int timeer) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        String time = format.format(new Date(Long.valueOf(timeer) * 1000L));
        Log.d("xxx-------->", "转换后时间: " + time);
        return time;
    }

    public String getYyyyMmDdText(int timeer) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_TEXT);
        String time = format.format(new Date(Long.valueOf(timeer) * 1000L));
        Log.d("xxx-------->", "转换后时间: " + time);
        Log.d("xxx-------->", "转换后毫秒: " + (Long.valueOf(timeer) * 1000L));
        return time;
    }

    /**
     * 自己设置 日历类型，传入 时间戳和类型即可
     *
     * @param timeer
     * @return
     */
    public String getTime(int timeer, String type) {
        SimpleDateFormat format = new SimpleDateFormat(type);
        String time = format.format(new Date(Long.valueOf(timeer) * 1000L));
        Log.d("xxx-------->", "获取到的时间: " + timeer);
        Log.d("xxx-------->", "转换后时间: " + time);
        Log.d("xxx-------->", "转换后毫秒: " + (Long.valueOf(timeer) * 1000L));
        return time;
    }

    /**
     * 根据毫秒时间戳来格式化字符串
     * 今天显示今天、昨天显示昨天、前天显示前天.
     * 早于前天的显示具体年-月-日，如2017-06-12；
     *
     * @param timeStamp 毫秒值
     * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
     */
    public static String getYesterToday(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        if (timeStamp >= todayStartMillis) {
            return "今天";
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if (timeStamp >= yesterdayStartMilis) {
            return "昨天";
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if (timeStamp >= yesterdayBeforeStartMilis) {
            return "前天";
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(new Date(timeStamp));
    }


}
